package com.lagou.util;

import java.nio.ByteBuffer;

/**
 * @ClassName LongByteUtil
 * @Description
 * @Author playboy
 * @Date 2021/6/9 6:00 下午
 * @Version 1.0
 **/
public class LongByteUtil {

	private static ByteBuffer buffer = ByteBuffer.allocate(8);

	//byte 数组与 long 的相互转换
	public static byte[] longToBytes(long x) {
		buffer.putLong(0, x);
		return buffer.array();
	}

	public static long bytesToLong(byte[] bytes) {
		buffer.clear();
		buffer.put(bytes, 0, bytes.length);
		buffer.flip();//need flip
		return buffer.getLong();
	}

}
